<template>
  <div class="getgoods">
    <div class="good-list">
      <!-- 商品列表项区域 -->
      <div class="mui-card" v-for="(item,i) in goodslist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="$store.getters.getGoodsSelected[item.id]"
              @change="selectedChanged(item.id,$store.getters.getGoodsSelected[item.id])"
            ></mt-switch>
            <img
              src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d880ff45a9a3b70527770e01521fc939.jpg?thumb=1&w=250&h=250&f=webp&q=90"
            />
            <div class="info">
              <h1>小米(Mi) 小米Note 16g双网通版</h1>
              <p>
                <span class="price">$666</span>
                <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                <a href="#" @click.prevent="remove(item.id,i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <span>总计(不含运费)</span>
            <span>
              已勾选商品
              <span class="clr">{{ $store.getters.getGoodsCountAndAmount.count }}</span>件,总价$
              <span class="clr">{{ $store.getters.getGoodsCountAndAmount.amount }}</span>
            </span>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//导入数字组件
import numbox from '../subcompontens/shop_bubbox.vue';
export default {
  data () {
    return {
      goodslist: [],

    }
  },
  created () {
    this.getgoodslist();
  },
  methods: {
    getgoodslist () {
      //获取到store 中的所有商品的ID,然后拼接出一个用逗号分隔的字符串
      var idAll = [];
      this.$store.state.car.forEach(item => idAll.push(item.id, item.count, item.price, item.selected));
      if (idAll.length <= 0) {
        return;
      }
      this.goodslist = idAll;
      //获取购物车商品列表
      //   this.$http.get('api/goods/getshopcarlist' + idAll.join(",")).then(result => {
      //     if (result.body.status == 0) {
      //       this.goodslist = result.body.message;
      //     }
      //   })
    },
    remove (id, index) {
      //点击删除,把商品从store 中根据传递的iD删除,同时当前组件中的goodslist中,对应要删除的那个商品,使用index来删除
      this.goodslist.splice(index, 1);
      this.$store.commit('removeFormCar', id);
    },
    selectedChanged (id, val) {
      this.$store.commit("updateGoodsSelected", { id, selected: val })
    }
  },
  components: {
    numbox
  }
}
</script>

<style lang="scss" scoped>
.getgoods {
  background-color: #eee;
  overflow: hidden;
  .good-list {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
    }

    img {
      height: 50px;
      width: 50px;
    }
    h1 {
      font-size: 13px;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
}

.jiesuan {
  display: flex;
  justify-content: space-between;
  align-content: center;
  .clr {
    color: red;
    font-size: 13px;
    font-weight: bold;
  }
}
</style>